<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useTaskStore } from './stores/taskStore'
import { useUserStore } from './stores/userStore'
import TaskBoard from './components/Taskboard.vue'
import MemberList from './components/MemberList.vue'
import Analytics from './components/Analytics.vue'
import Navbar from './components/Navbar.vue'

const router = useRouter()
const taskStore = useTaskStore()
const userStore = useUserStore()
const activeTab = ref('tasks')

// 监听用户状态变化
watch(
  () => userStore.currentUser,
  (newVal) => {
    if (!newVal) {
      router.push('/login')
    }
  }
)

// 初始化检查
onMounted(() => {
  if (!userStore.currentUser) {
    router.push('/login')
  }
})
</script>

<template>
  <div class="app-container">
    <Navbar v-if="userStore.currentUser" />
    
    <template v-if="userStore.currentUser">
      <header class="app-header">
        <div class="header-content">
          <h1>智能任务管理系统</h1>
          <nav class="tabs">
            <button 
              v-for="tab in ['tasks', 'members', 'analytics']"
              :key="tab"
              :class="{ active: activeTab === tab }" 
              @click="activeTab = tab"
            >
              {{ 
                tab === 'tasks' ? '任务看板' : 
                tab === 'members' ? '成员管理' : '数据分析' 
              }}
            </button>
          </nav>
        </div>
      </header>

      <main class="app-main">
        <div class="container">
          <transition name="fade" mode="out-in">
            <component 
              :is="
                activeTab === 'tasks' ? TaskBoard : 
                activeTab === 'members' ? MemberList : Analytics
              " 
              :key="activeTab" 
            />
          </transition>
        </div>
      </main>
    </template>

    <router-view v-else />
  </div>
</template>

<style scoped>
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.app-header {
  background: linear-gradient(135deg, #42b983 0%, #3498db 100%);
  color: white;
  padding: 1rem 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
}

.app-header h1 {
  margin: 0;
  font-size: 1.8rem;
  text-align: center;
}

.tabs {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  justify-content: center;
}

.tabs button {
  padding: 0.5rem 1.5rem;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  border-radius: 20px;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
}

.tabs button.active {
  background: white;
  color: #42b983;
  font-weight: bold;
}

.app-main {
  flex: 1;
  padding: 2rem 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>